<?php

# page data class include functions which are related to data link with sql
class PageData{
	
	/**
	*getChildrenPagesWithAccessableGroups() according to given parent id, this function will get all children pages of this parent and their accessable groups
	*
	*@param $parentID which is id of given parent 
	*/
	
	function getChildrenPagesWithAccessableGroups($parentID){
		
		global $PROJECT_DB_TABLES;
		
		# Create container
		$pages = new Pages();
	}

	/**
	 * getAllPages() gets the ID, name and parent of all the pages that are active, and places them in a Pages() object
	*/
	function getAllPages() {
		
		# Globals
		global $SITE_ID;
		global $PROJECT_DB_TABLES;
	
		# Create container
		$pages = new Pages();
	
		# SQL
		$sql = "SELECT p_id, p_name, p_parent, p_status, p_sort, p_section, p_accesstype, p_access, p_innavigation,p_content FROM " . $PROJECT_DB_TABLES["pages"] . " WHERE p_status < 3 AND p_status>=0 AND p_section = '" . $SITE_ID . "' AND p_accessdatestart <= '" . getFullDate() . "' AND p_accessdateend >= '" . getFullDate() . "' ORDER BY p_sort ASC";
	
		# Get record
		$result = getRecord($sql, "p_id");
	
		# Add pages
		foreach ($result as $key => $page_array) {
			
			$obPage = new Page();
			
			$obPage -> loadPageData($page_array);
			
			$pages -> addPage($obPage);
		}
	
		# Return
		return $pages;
	}
	
	/**
	 * getSectionPages() gets the ID, name and parent of all the pages that are active, and places them in a Pages() object
	 * @param $section is name of section
	 * @param $parentID is id of parent layer
	 $ @return $pages which is object of pages class.
	*/
	function getSectionPages($section = '', $parentID = '') {
	
		global $PROJECT_DB_TABLES;
		global $USER;
		
		# Create container
		$pages = new Pages();
	
		# SQL
		$sql = "SELECT p.p_id AS p_id, p.p_name AS p_name, p.p_parent AS p_parent, p.p_status AS p_status, p.p_sort AS p_sort, p.p_section AS p_section, p.p_accesstype AS p_accesstype, p.p_accessdatestart AS p_accessdatestart, p.p_accessdateend AS accessdateend, p.p_access AS p_access, p.p_innavigation AS p_innavigation,p.p_layer AS p_layer, pp.pp_p_id AS pp_p_id ";
		
		$sql .= "FROM ".$PROJECT_DB_TABLES['pages']." p ";
		
		$sql .= "LEFT JOIN ".$PROJECT_DB_TABLES['pagepath']." pp ";
		
		$sql .= "ON p.p_id = pp.pp_path ";
		
		$sql .= "WHERE p.p_parent = '".$parentID."' ";
		
		$sql .= "AND p.p_section =  '".$section."' ";
		
		if(!$USER -> getUserGroup() -> hasGroupByName("Developers")){
		
			$sql .= "AND p.p_status >0 ";
			
			$sql .= "AND p.p_accessdatestart <= '".getFullDate()."' ";
			
			$sql .= "AND p.p_accessdateend >= '".getFullDate()."' ";
		}

		$sql .= "ORDER BY p.p_sort ASC "; 
		
		//$sql = "SELECT p_id, p_name, p_parent, p_status, p_sort, p_section, p_accesstype,p_accessdatestart,p_accessdateend, p_access, p_innavigation,pp_path FROM " . $PROJECT_DB_TABLES["pages"] . " WHERE p_status < 3 AND p_status > 0 AND p_section = '" . $section . "' AND p_accessdatestart <= '" . getFullDate() . "' AND p_accessdateend >= '" . getFullDate() . "' ORDER BY p_sort ASC";
		
		//echo $sql."<br/>";
		# Get record
		$result = getRecord($sql);
	
		# Add pages
		foreach ($result as $page_array) {
			
			if($pages -> isPageExist($page_array['p_id'])){
				
				$pages -> getPage($page_array['p_id']) -> loadPageData($page_array);
			}
			else{
			
				$obPage = new Page();
				
				$obPage -> loadPageData($page_array);
				
				$pages -> addPage($obPage);
			}
		}
		
		//print_r($pages);
	
		# Return
		return $pages;
	}
	
	/**
	*getRecycledPages() get pages for recycled bin
	*@return 
	*/
	public function getRecycledPages(){
		
		global $PROJECT_DB_TABLES;
		
		$sql = "SELECT * FROM ".$PROJECT_DB_TABLES["pages_junk"]." ORDER BY j_time";//pages_junk
		
		$result = getRecord($sql, "p_id");
		
		return $result;
	}
	
	/**
	 * getParentPages() gets the ID, name and parent of all the pages that are active, and places them in a Pages() object
	*/
	function getParentPages($parent) {
	
		global $PROJECT_DB_TABLES;
		
		# Create container
		$pages = new Pages();
	
		# SQL
		$sql = "SELECT p_id, p_name, p_parent, p_status, p_sort, p_section, p_accesstype, p_access, p_innavigation FROM " . $PROJECT_DB_TABLES["pages"] . " WHERE p_status < 3 AND p_status>0 AND p_parent = '" . $parent . "' AND p_accessdatestart <= '" . getFullDate() . "' AND p_accessdateend >= '" . getFullDate() . "' ORDER BY p_sort ASC";
	
		# Get record
		$result = getRecord($sql, "p_id");
	
		# Add pages
		foreach ($result as $key => $page_array) {
			$pages -> addPage(new Page($key, $page_array));
		}
	
		# Return
		return $pages;
	}
	
	
	/**
	 * getPageDetails() gets the full page details of a page as a Page object
	 *
	 * @param page_id is the ID of the page
	 *
	 * @return a Page object containing all the parameters
	*/
	function getPageDetails($page_id) {
	
		# globals
		global $PROJECT_DB_TABLES;
	
		# Page
		$page = new Page();
	
		# SQL
		$sql = "SELECT * FROM " . $PROJECT_DB_TABLES["pages"] . " WHERE p_id = '" . $page_id . "'";
	
		# Get record
		$result = getRecord($sql);
	
		# If results returned OK
		if ($result !== false && count($result) > 0) {
	
			$page -> loadPageData($result[0]);
		}
	
		# Return
		return $page;
	}
	
	/**
	 * getPageDetails() gets the full page details of a page as a Page object
	 *
	 * @param page_id is the ID of the page
	 *
	 * @return a Page object containing all the parameters
	*/
	function getPageDetailsWithPath($page_id) {

		# globals
		global $PROJECT_DB_TABLES;
	
		# Page
		$page = new Page();
	
		# SQL
		$sql = "SELECT p1.p_id AS p_id, p1.p_name AS p_name, p1.p_parent AS p_parent, p1.p_status AS p_status, p1.p_sort AS p_sort, p1.p_section AS p_section, p1.p_accesstype AS p_accesstype, p1.p_accessdatestart AS p_accessdatestart, p1.p_accessdateend AS accessdateend, p1.p_access AS p_access, p1.p_innavigation AS p_innavigation, p2.p_layer AS p_layer_2, pp.pp_path AS pp_path ";
		
		$sql .= "FROM " . $PROJECT_DB_TABLES["pages"] . " p1 ";
		
		$sql .= "LEFT JOIN ".$PROJECT_DB_TABLES["pagepath"]." pp "; 
		
		$sql .= "ON p1.p_id = pp.pp_p_id "; 
		
		$sql .= "LEFT JOIN ".$PROJECT_DB_TABLES["pages"]." p2 "; 
		
		$sql .= "ON pp.pp_path = p2.p_id "; 
		
		$sql .= "WHERE p1.p_id = '" . $page_id . "' ";
		
		echo $sql;
	
		# Get record
		$result = getRecord($sql);

		foreach($result as $value){
		
			$page -> loadPageData($value);
		}
	
		# Return
		return $page;
	}
	
	/**
	*getSections() get distinct section lists 
	*@param $recycleFlag decide should this function return Recycle bin or not
	*@return $arrSections which is array of sections
	*/
	public function getSections($recycleFlag){
		
		# globals
		global $PROJECT_DB_TABLES;
		global $USER;
		
		$arrSections = array();
		
		//$sql = "SELECT distinct p_section FROM ".$PROJECT_DB_TABLES['pages']." ORDER BY p_section";
		
		//$result = getRecord($sql);
		
		//foreach($result as $value){
			
			//$arrSections[] = $value['p_section'];
		//}
		
				# globals
		global $PROJECT_DB_TABLES;
		global $USER;
		
		$arrSections = array();
		
		//$sql = "SELECT distinct p_section FROM ".$PROJECT_DB_TABLES['pages']." ORDER BY p_section";
		
		//$result = getRecord($sql);
		
		//foreach($result as $value){
			
			//$arrSections[] = $value['p_section'];
		//}
		
		$arrSections = getSectionNames();
		
		if($USER -> getUserGroup() -> hasGroupByName("Developers") && $recycleFlag){
			
			$arrSections[] = "Recycle Bin";

		}
		
		return $arrSections;
	}
	
	/**
	*getNavigationPages() get pages which are navigatable
	*
	*@param $section is the page section
	*@return $pages is object of pages class with loading data
	*/
	public function getNavigationPages($section = ""){
		
		# globals
		global $PROJECT_DB_TABLES;
		global $USER;
		
		$sql = "SELECT p.p_id AS p_id, p.p_name AS p_name, p.p_parent AS p_parent, p.p_status AS p_status, p.p_sort AS p_sort, p.p_section AS p_section, p.p_accesstype AS p_accesstype, p.p_accessdatestart AS p_accessdatestart, p.p_accessdateend AS accessdateend, p.p_access AS p_access, p.p_innavigation AS p_innavigation, pg.pg_g_id as g_id ";
		
		$sql .= "FROM ".$PROJECT_DB_TABLES['pages']." p ";
		
		$sql .= "LEFT JOIN ".$PROJECT_DB_TABLES['pagegroup']." pg ";
		
		$sql .= "ON p.p_id = pg.pg_p_id ";
		
		//$sql .= "LEFT JOIN ".$PROJECT_DB_TABLES['groups']." g ";
		
		//$sql .= "ON g.g_id = pg.pg_g_id ";
		
		$sql .= "WHERE p.p_innavigation = 1 ";
		
		$sql .= "AND p.p_section = 'admin' ";
		
		$sql .= "AND p.p_parent = ''";
		
		$sql .= "AND p.p_status >0 ";
			
		$sql .= "AND p.p_accessdatestart <= '".getFullDate()."' ";
			
		$sql .= "AND p.p_accessdateend >= '".getFullDate()."' ";
		
		$sql .= "AND p.p_section = '".$section."'";
		
		$result = getRecord($sql);
		
		//echo $sql;
		
		$pages = new Pages();
		
		# Add pages
		foreach ($result as $key => $page_array) {
			
			if($pages -> isPageExist($page_array['p_id'])){
				
				$pages -> getPage($page_array['p_id']) -> loadPageData($page_array);
			}
			else{
			
				$obPage = new Page();
				
				$obPage -> loadPageData($page_array);
				
				$pages -> addPage($obPage);
			}
		}
	
		# Return
		return $pages;
		
	}
	
	/**
	*getPageListAtDepth() get page list according to given page lists
	*
	*
	*/
	public function getPageListAtDepth($section = "",$layer = 0){
		
		# globals
		global $PROJECT_DB_TABLES;
		
		$sql = "SELECT p.p_id AS p_id, p.p_name AS p_name, p.p_parent AS p_parent, p.p_status AS p_status, p.p_sort AS p_sort, p.p_section AS p_section, p.p_accesstype AS p_accesstype, p.p_accessdatestart AS p_accessdatestart, p.p_accessdateend AS accessdateend, p.p_access AS p_access, p.p_innavigation AS p_innavigation ";
		
		$sql .= "FROM ".$PROJECT_DB_TABLES['pages']." p ";
		
		$sql .= "WHERE p_section = '".$section."' ";
		
		$sql .= "AND p_layer = ".$layer." ";
		
		$sql .= "ORDER BY p_sort";
		
		$result = getRecord($sql);
		
		$pages = new Pages();
		
		# Add pages
		foreach ($result as $key => $page_array) {
			
			if($pages -> isPageExist($page_array['p_id'])){
				
				$pages -> getPage($page_array['p_id']) -> loadPageData($page_array);
			}
			else{
			
				$obPage = new Page();
				
				$obPage -> loadPageData($page_array);
				
				$pages -> addPage($obPage);
			}
		}
	
		# Return
		return $pages;
	}
	
	/**
	*getPageAtDepth() get page id according to depth of page
	*@param $page_id is page id
	*@param $depth is depth of pages layer
	*@return true/false
	*/
	public function getPageAtDepth($page_id = "" ,$depth = 0){
		
		# globals
		global $PROJECT_DB_TABLES;
		
		$sql = "SELECT p_id FROM ".$PROJECT_DB_TABLES['pages']." WHERE p_id = '".$page_id."' AND p_layer = 0";
		
		$result = getRecord($sql);
		
		if(count($result)>0){
			
			return $result[0]["p_id"];
		}
		
		return false;
	}
}
?>